var materialCategoryList = null;
var materialCategoryID = null;
var parentid_search = null;
var lei = null;
var id;
//初始化界面
$(function () {
    //初始化系统基础信息
    initSystemData(1);
    initSelectInfo(1);
    initTableData();
    ininPager();
    initForm();
});

//初始化系统基础信息
function initSystemData(parentid_search) {
    $.ajax({
        type: "post",
        url: baseURL+"/sys/materialcategory/getBasicData",
        data: ({
            ParentId: parentid_search
        }),
        //设置为同步
        async: false,
        dataType: "json",
        success: function (systemInfo) {
            materialCategoryList = systemInfo.materialCategoryList;
            var msgTip = systemInfo.code;
            if (msgTip != 0) {
                $.messager.alert('提示', '查找商品类别异常,请与管理员联系！', 'error');
                return;
            }
        }
    });
}

//初始化页面选项卡
function initSelectInfo(lei) {
    var options = "";

    if (materialCategoryList != null) {
        options = "";
        for (var i = 0; i < materialCategoryList.length; i++) {
            var materialCategory = materialCategoryList[i];
            if (0 == i) {
                materialCategoryID = materialCategory.id;
            }
            options += '<option value="' + materialCategory.id + '">' + materialCategory.name + '</option>';
        }
        //$("#ParentId").empty().append(options);
        if (lei == 1) {
            $("#searchParentId_f").empty().append('<option value="">全部</option>').append(options);
        }
        else if (lei == 2) {
            $("#searchParentId_s").empty().append('<option value="">全部</option>').append(options);
        }
        else if (lei == 3) {
            $("#searchParentId_t").empty().append('<option value="">全部</option>').append(options);
        }
        else if (lei == 11) {
            $("#ParentId_f").empty().append('<option value="">全部</option>').append(options);
        }
        else if (lei == 12) {
            $("#ParentId_s").empty().append('<option value="">全部</option>').append(options);
        }
    }
}

$("#searchParentId_f").change(
    function () {
        var parentid_search = $("#searchParentId_f").val();
        if (parentid_search != '') {
            initSystemData(parentid_search);
            initSelectInfo(2);
        }
    }
);

$("#searchParentId_s").change(
    function () {
        var parentid_search = $("#searchParentId_s").val();
        if (parentid_search != '') {
            initSystemData(parentid_search);
            initSelectInfo(3);
        }
    }
);

$("#CategoryLevel").change(
    function () {
        var CategoryLevel = $("#CategoryLevel").val();
        if (CategoryLevel == 1) {
            $("#ParentId_f").empty();
            $("#ParentId_s").empty();
        }
        else if (CategoryLevel == 2) {
            initSystemData(1);
            initSelectInfo(11);
            $("#ParentId_s").enabled = false;
        }
        else if (CategoryLevel == 3) {
            initSystemData(1);
            initSelectInfo(11);
        }
    }
);

$("#ParentId_f").change(
    function () {
        var ParentId_f = $("#ParentId_f").val();
        initSystemData(ParentId_f);
        initSelectInfo(12);
    }
);

//防止表单提交重复
function initForm() {
    $('#materialCategoryFM').form({
        onSubmit: function () {
            return false;
        }
    });
}

//初始化表格数据
function initTableData() {
    $('#tableData').datagrid({
        //title:'商品类别列表',
        //iconCls:'icon-save',
        //width:700,
        height: heightInfo,
        nowrap: false,
        rownumbers: false,
        //动画效果
        animate: false,
        //选中单行
        singleSelect: true,
        collapsible: false,
        selectOnCheck: false,
        //fitColumns:true,
        //单击行是否选中
        //checkOnSelect : false,
        url: baseURL+'/sys/materialcategory/findBy?pageSize=' + initPageSize + '&ParentId=1',
        pagination: true,
        //交替出现背景
        striped: true,
        //loadFilter: pagerFilter,
        pageSize: initPageSize,
        pageList: initPageNum,
        columns: [[
            {field: 'Id', width: 35, align: "center", checkbox: true},
            {title: '名称', field: 'Name', width: 250},
            {
                title: '操作', field: 'op', align: "center", width: 130, formatter: function (value, rec) {
                    var str = '';
                    var rowInfo = rec.Id + 'AaBb' + rec.ParentId + 'AaBb' + rec.CategoryLevel + 'AaBb' + rec.Name;
                    if (1 == value) {
                        str += '<img src="'+baseURL+'/statics/plugins/easyui-1.3.5/themes/icons/pencil.png" style="cursor: pointer;" onclick="editMaterialCategory(\'' + rowInfo + '\');"/>&nbsp;<a onclick="editMaterialCategory(\'' + rowInfo + '\');" style="text-decoration:none;color:black;" href="javascript:void(0)">编辑</a>&nbsp;&nbsp;';
                        str += '<img src="'+baseURL+'/statics/plugins/easyui-1.3.5/themes/icons/edit_remove.png" style="cursor: pointer;" onclick="deleteMaterialCategory(' + rec.Id + ');"/>&nbsp;<a onclick="deleteMaterialCategory(' + rec.Id + ');" style="text-decoration:none;color:black;" href="javascript:void(0)">删除</a>&nbsp;&nbsp;';
                    }
                    return str;
                }
            }
        ]],
        toolbar: [
            {
                id: 'addMaterialCategory',
                text: '增加',
                iconCls: 'icon-add',
                handler: function () {
                    addMaterialCategory();
                }
            },
            '-',
            {
                id: 'deleteMaterialCategory',
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    batDeleteMaterialCategory();
                }
            }
        ],
        onLoadError: function () {
            $.messager.alert('页面加载提示', '页面加载异常，请稍后再试！', 'error');
            return;
        }
    });
}

//初始化键盘enter事件
$(document).keydown(function (event) {
    //兼容 IE和firefox 事件
    var e = window.event || event;
    var k = e.keyCode || e.which || e.charCode;
    //兼容 IE,firefox 兼容
    var obj = e.srcElement ? e.srcElement : e.target;
    //绑定键盘事件为 id是指定的输入框才可以触发键盘事件 13键盘事件 ---遗留问题 enter键效验 对话框会关闭问题
    if (k == "13" && (obj.id == "CategoryLevel" || obj.id == "Name")) {
        $("#saveMaterialCategory").click();
    }
    //搜索按钮添加快捷键
    if (k == "13" && (obj.id == "searchParentId" || (obj.id == "searchName"))) {
        $("#searchBtn").click();
    }
});

//分页信息处理
function ininPager() {
    try {
        var opts = $("#tableData").datagrid('options');
        var pager = $("#tableData").datagrid('getPager');
        pager.pagination({
            onSelectPage: function (pageNum, pageSize) {
                opts.pageNumber = pageNum;
                opts.pageSize = pageSize;
                pager.pagination('refresh',
                    {
                        pageNumber: pageNum,
                        pageSize: pageSize
                    });
                showMaterialCategoryDetails(pageNum, pageSize);
            }
        });
    }
    catch (e) {
        $.messager.alert('异常处理提示', "分页信息异常 :  " + e.name + ": " + e.message, 'error');
    }
}

//删除商品类别信息
function deleteMaterialCategory(materialCategoryID) {
    var ids=[];
    ids.push(materialCategoryID)
    $.messager.confirm('删除确认', '确定要删除此商品类别信息吗？', function (r) {
        if (r) {
            $.ajax({
                type: "post",
                url: baseURL+"/sys/materialcategory/delete",
                dataType: "json",
                contentType:'application/json',
                data: JSON.stringify(ids),
                success: function (tipInfo) {
                    var msg = tipInfo.code;
                    if (msg ==0) {
                        //加载完以后重新初始化
                        $("#searchBtn").click();
                    }
                    else
                        $.messager.alert('删除提示', '删除商品类别信息失败，请稍后再试！', 'error');
                },
                //此处添加错误处理
                error: function () {
                    $.messager.alert('删除提示', '删除商品类别信息异常，请稍后再试！', 'error');
                    return;
                }
            });
        }
    });
}

//批量删除商品类别
function batDeleteMaterialCategory() {
    var idss = [];
    var row = $('#tableData').datagrid('getChecked');
    if (row.length == 0) {
        $.messager.alert('删除提示', '没有记录被选中！', 'info');
        return;
    }
    if (row.length > 0) {
        $.messager.confirm('删除确认', '确定要删除选中的' + row.length + '条商品类别信息吗？', function (r) {
            if (r) {
                var ids = "";
                for (var i = 0; i < row.length; i++) {
                    if (i == row.length - 1) {
                        ids += row[i].Id;
                        idss.push( row[i].Id);
                        break;
                    }
                    //alert(row[i].id);
                    ids += row[i].Id + ",";
                    idss.push( row[i].Id);
                }
                $.ajax({
                    type: "post",
                    url: baseURL+"/sys/materialcategory/delete",
                    dataType: "json",
                    contentType:'application/json',
                    async: false,
                    data: JSON.stringify(idss),
                    success: function (tipInfo) {
                        var code = tipInfo.code;
                        if (code ==0) {
                            //加载完以后重新初始化
                            $("#searchBtn").click();
                            $(":checkbox").attr("checked", false);
                        }
                        else
                            $.messager.alert('删除提示', '删除商品类别信息失败，请稍后再试！', 'error');
                    },
                    //此处添加错误处理
                    error: function () {
                        $.messager.alert('删除提示', '删除商品类别信息异常，请稍后再试！', 'error');
                        return;
                    }
                });
            }
        });
    }
}

//增加
var url;
var materialCategoryID = 0;
//保存编辑前的名称
var orgMaterialCategory = "";

function addMaterialCategory() {
    $('#materialCategoryFM').form('clear');
    $('#materialCategoryDlg').dialog('open').dialog('setTitle', '<img src="'+baseURL+'/statics/plugins/easyui-1.3.5/themes/icons/edit_add.png"/>&nbsp;增加商品类别信息');
    $(".window-mask").css({width: webW, height: webH});
    $("#Name").val("").focus();

    orgMaterialCategory = "";
    materialCategoryID = 0;
    url = baseURL+'/sys/materialcategory/save';
}

//保存信息
$("#saveMaterialCategory").unbind().bind({
    click: function () {
        if (!$('#materialCategoryFM').form('validate'))
            return;
        else {
            var parent = 1;
            if ($("#ParentId_f").val() != "" && $("#ParentId_f").val() != null) {
                parent = $("#ParentId_f").val();
            }
            if ($("#ParentId_s").val() != "" && $("#ParentId_s").val() != null) {
                parent = $("#ParentId_s").val();
            }
            $.ajax({
                type: "post",
                url: url,
                dataType: "json",
                async: false,
                contentType:'application/json',
                data: JSON.stringify({
                    parentid: parent,
                    categorylevel: $("#CategoryLevel").val(),
                    name: $.trim($("#Name").val()),
                    id:id
                }),
                success: function (tipInfo) {
                    if (tipInfo) {
                        $('#materialCategoryDlg').dialog('close');

                        var opts = $("#tableData").datagrid('options');
                        showMaterialCategoryDetails(opts.pageNumber, opts.pageSize);
                    }
                    else {
                        $.messager.show({
                            title: '错误提示',
                            msg: '保存商品类别信息失败，请稍后重试!'
                        });
                    }
                },
                //此处添加错误处理
                error: function () {
                    $.messager.alert('提示', '保存商品类别信息异常，请稍后再试！', 'error');
                    return;
                }
            });
        }
    }
});

//编辑信息
function editMaterialCategory(materialCategoryTotalInfo) {
    var materialCategoryInfo = materialCategoryTotalInfo.split("AaBb");

    $("#ParentId").focus().val(materialCategoryInfo[1]);
    $("#CategoryLevel").val(materialCategoryInfo[2]);
    $("#Name").val(materialCategoryInfo[3]);

    //orgMaterialCategory = materialCategoryInfo[1];
    $('#materialCategoryDlg').dialog('open').dialog('setTitle', '<img src="'+baseURL+'/statics/plugins/easyui-1.3.5/themes/icons/pencil.png"/>&nbsp;编辑商品类别信息');
    $(".window-mask").css({width: webW, height: webH});
    materialCategoryID = materialCategoryInfo[0];
    //焦点在名称输入框==定焦在输入文字后面
    $("#Name").val("").focus().val(materialCategoryInfo[3]);
    url = baseURL+'/sys/materialcategory/update?materialCategoryID=' + materialCategoryInfo[0];
    id=materialCategoryInfo[0]
}

//搜索处理
$("#searchBtn").unbind().bind({
    click: function () {
        showMaterialCategoryDetails(1, initPageSize);
        var opts = $("#tableData").datagrid('options');
        var pager = $("#tableData").datagrid('getPager');
        opts.pageNumber = 1;
        opts.pageSize = initPageSize;
        pager.pagination('refresh',
            {
                pageNumber: 1,
                pageSize: initPageSize
            });
    }
});


function showMaterialCategoryDetails(pageNo, pageSize) {
    var parent = 1;
    if ($("#searchParentId_f").val() != "" && $("#searchParentId_f").val() != null) {
        parent = $("#searchParentId_f").val();
    }
    if ($("#searchParentId_s").val() != "" && $("#searchParentId_s").val() != null) {
        parent = $("#searchParentId_s").val();
    }
    if ($("#searchParentId_t").val() != "" && $("#searchParentId_t").val() != null) {
        parent = $("#searchParentId_t").val();

    }
    $.ajax({
        type: "post",
        url: baseURL+"/sys/materialcategory/findBy",
        dataType: "json",
        data: ({
            Name: $.trim($("#searchName").val()),
            ParentId: parent,
            pageNo: pageNo,
            pageSize: pageSize
        }),
        success: function (data) {
            $("#tableData").datagrid('loadData', data);
        },
        //此处添加错误处理
        error: function () {
            $.messager.alert('查询提示', '查询数据后台异常，请稍后再试！', 'error');
            return;
        }
    });
}

//重置按钮
$("#searchResetBtn").unbind().bind({
    click: function () {
        $("#searchParentId_f").val("");
        $("#searchParentId_s").val("");
        $("#searchParentId_t").val("");
        //加载完以后重新初始化
        $("#searchBtn").click();
    }
});
